<TITLE>Reading</TITLE>

<H1>Reading a file</H1>

Most users simply want read access for a player toy or something.  A
good place to start is before opening the file, making sure it is
Quicktime with quicktime_check_sig().<P>

<CODE>
quicktime_check_sig("path");<P>
</CODE>

This returns 1 if it looks like a Quicktime file or 0 if it doesn't. 
Then you can open the file as described in <A
HREF="opening.html">opening.html</A>.<P>

Next get the number of tracks for each media type in the file:<P>

<CODE>
int quicktime_video_tracks(quicktime_t *file);<BR>
int quicktime_audio_tracks(quicktime_t *file);
</CODE>
<P>


While Quicktime can store multiple video tracks, the audio track count
is a bit more complicated.  Usually you'll only encounter a single
audio track.  Inside the audio track is a variable number of channels. 
To get the channel count call:<P>

<CODE>
int quicktime_track_channels(quicktime_t *file, int track);
</CODE>
<P>

With the track parameter set to track 0.  Many routines require a
<B>track</B> parameter to specify the track to operate on.  Tracks are
always numbered from 0 to the total number of tracks - 1 for the
particular media type.<P>

Audio tracks are numbered from 0 to the total number of audio tracks -
1.  But like I said, you'll probably never encounter an audio track
higher than 0.  Other routines you might find useful for getting audio
information are:<P>

<CODE>
long quicktime_sample_rate(quicktime_t *file, int track);<BR>
long quicktime_audio_length(quicktime_t *file, int track);<BR>
</CODE>
<P>

quicktime_audio_length gives you the total number of samples.  The
sample rate is samples per second.<P>

Routines you'll never use unless you want to write a codec are:<P>

<CODE>
char* quicktime_audio_compressor(quicktime_t *file, int track);<BR>
int quicktime_audio_bits(quicktime_t *file, int track);<BR>
</CODE>
<P>

The audio compressor call returns a 4 byte array identifying the data
compression of the track.  These identifiers are 4 alphanumeric
characters which go along with one of the #defines in quicktime.h.  The
bits parameter returns the number of bits in a sample.<P>

The most interesting contents of a Quicktime file are of course the
video tracks.  Quicktime stores multiple video tracks.<P>

The available video information for each video track is:<P>


<CODE>
long quicktime_video_length(quicktime_t *file, int track);<BR>
int quicktime_video_width(quicktime_t *file, int track);<BR>
int quicktime_video_height(quicktime_t *file, int track);<BR>
float quicktime_frame_rate(quicktime_t *file, int track);<BR>
long quicktime_frame_size(quicktime_t *file, long frame, int track);<BR>
int quicktime_video_depth(quicktime_t *file, int track);<BR>
</CODE>
<P>



Tracks are numbered 0 to the total number of tracks - 1.  The video
length is in frames.  The width and height are in pixels.  The frame
rate is in frames per second.  Depth returns the total number of bits
per pixel.  These are not the number of bits per pixel on disk but the
number of bits per pixel in the decompressed data returned by one of
the video decoding routines.  The only two values Quicktime for Linux
returns are 24 and 32 and the 32 bit depth is only supported in PNG and
RGB format.  There's no reason to use 16 or 8.  A depth of 24 means a
video row from a decoding routine is in RGBRGBRGB packed bytes.  A
depth of 32 means video data is in RGBARGBARGBA packed bytes with alpha
channels.<P>

To get a four byte compressor type for the track issue:<P>

<CODE>
char* quicktime_video_compressor(quicktime_t *file, int track);<BR>
</CODE>
<P>

Unless you get a really nihilistic file for reading, you can safely
assume the encoding scheme for track 0 of any file is the same for all
tracks of that media type.<P>

<H1>Decoding video</H1>


The library decodes compressed video frames into rows of unsigned RGB
bytes or RGBA bytes, depending on the depth.  Then it gives you an
array of row pointers.  First issue<P>

<CODE>
int quicktime_supported_video(quicktime_t *file, int track);<BR>
</CODE>
<P>

to find out if the data for the track can be decoded by the library. 
This returns 1 if it is and 0 if it isn't supported.<P>

Then use<P>

<CODE>
int quicktime_decode_video(quicktime_t *file, unsigned char **row_pointers, int track);<BR>
</CODE>
<P>

to decompress a frame at the current position of the track into
**row_pointers and advance the current position.  The array of rows
must have enough space allocated for the entire frame.  For each row
that's width * depth / 8 bytes.  For more about positioning go to <A
HREF="positioning.html">positioning</A><P>

There are other routines for reading compressed data and chunks, but
unless you want to write a codec, you'd better focus on more important
things.<P>


<H1>Decoding audio</H1>

For reading audio, first use:<P>

<CODE>
int quicktime_supported_audio(quicktime_t *file, int track);<BR>
</CODE>
<P>

To determine if the audio can be decompressed by the library.  This
returns 1 if it is and 0 if it isn't supported.  Then use<P>

<CODE>
int quicktime_decode_audio(quicktime_t *file, QUICKTIME_INT16 *output_i, float *output_f, long samples, int channel);<BR>
</CODE>
<P>


To read a buffer's worth of samples for a single channel starting at
the current position in the track.  Notice this command takes a channel
argument not a track argument.  The channel argument is automatically
converted into a track and channel.  Positioning information is
automatically taken from the appropriate track and advanced for all the
channels in the track.<P>

Notice the QUICKTIME_INT16* and float* parameters.  This call can
either return a buffer of int16 samples or float samples.  The argument
for the data format you want should be passed a preallocated buffer big
enough to contain the sample range while the undesired format should be
passed NULL.  For a buffer of float samples you would say<P>

<CODE>
result = quicktime_decode_audio(file, NULL, output_f, samples, channel);<BR>
</CODE>
<P>

For a buffer of signed int16 samples you would say<P>

<CODE>
result = quicktime_decode_audio(file, output_i, NULL, samples, channel);<BR>
</CODE>
<P>

The data format you don't want should be passed a NULL.  The decoder
automatically fills the appropriate buffer.  Floating point samples are
from -1 to 0 to 1.<P>


When you're done reading, call 

<CODE>
quicktime_close(quicktime_t *file);<BR>
</CODE>
<P>

<H1>Reading raw video</H1>

<CODE>
long quicktime_read_frame(quicktime_t *file, unsigned char *video_buffer, int track);
</CODE>
<P>

<B>quicktime_read_frame</B> reads one frame worth of raw data from your
current position on the specified video track and returns the number of
bytes in the frame.  You have to make sure the buffer is big enough for
the frame.   A return value of 0 means error.<P>

<CODE>
long quicktime_frame_size(quicktime_t *file, long frame, int track);
</CODE>
<P>

gives up the number of bytes in the specified frame in the specified
track even if you haven't read the frame yet.  Frame numbers start on
0.<P>

Now some of you are going to want to read frames directly from a file
descriptor using another library like libjpeg or something.  To read a
frame directly start by calling <P>

<CODE>
	int quicktime_read_frame_init(quicktime_t *file, int track);
</CODE><P>

to initialize the input.

Then read your raw, compressed data from a file descriptor given by
<P>

<CODE>
	FILE* quicktime_get_fd(quicktime_t *file);
</CODE>
<P>

End the frame read operation by calling<P>

<CODE>
	int quicktime_read_frame_end(quicktime_t *file, int track);
</CODE>
<P>

You can get the file descriptor any time the file is opened, not just
when reading or writing, but you must call the init and end routines to
read a frame.

<H1>Reading raw audio</H1>

These commands are good for reading raw sample data.  They should only
be used for codecs not supported in the library and only work for
interleaved, linear PCM data.<P>

<CODE>
long quicktime_read_audio(quicktime_t *file, char *audio_buffer, long samples, int track);
</CODE>
<P>

<B>quicktime_read_audio</B> requires a number of samples of raw audio data to
read.  Then it reads that corresponding number of bytes on the
specified track and returns the equivalent number of bytes read or 0 if
error.  The data read is PCM audio data of interleaved channels
depending on the format of the track.

Be aware that Quicktime for Linux tries to guess the number of bytes by
the codec type, so attempts to read most nonlinear codecs will crash.

